{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3dddceb0",
   "metadata": {},
   "source": [
    "-------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44b9aac2",
   "metadata": {},
   "source": [
    "* 2022/09/20 /week05\n",
    "* python 课堂笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa8d90a8",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d14f6fc",
   "metadata": {},
   "source": [
    "# 本周内容\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2e76fe3",
   "metadata": {},
   "source": [
    "> 1.有序的数据结构之列表\n",
    "\n",
    "> 2.用户输入\n",
    "\n",
    "> 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c3b4e07f",
   "metadata": {},
   "outputs": [],
   "source": [
    "网3_01_name = \"Mary\"\n",
    "\n",
    "# 思考：如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "# 思考：处理数据会不会比较麻烦？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "17713b06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mary,Alex,Jack,黄晓明\n"
     ]
    }
   ],
   "source": [
    "网3_name_list = \"Mary,Alex,Jack,黄晓明\"\n",
    "print(网3_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1bacbac7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'lex,'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思考2：处理数据会不会比较麻烦？\n",
    "网3_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6a1bc399",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思考3： 我想知道这个名单有多少个人名？\n",
    "len(网3_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d6ebc7b",
   "metadata": {},
   "source": [
    "## 1. 引入列表的概念\n",
    "* Lists might contain items of different types, but usually the items all  have the same type\n",
    "> 注释：我们已经学习了 数值 和 字符，list可以放数值和字符，但是通常要么一起放数值，要么一起放字符串。\n",
    "\n",
    ">总结一些通用的有序数列结构的方法：\n",
    ">> 1.取值：通过索引值取出对应的内容\n",
    ">> 2.切片：通过索引值区间去除对应的一些内容\n",
    ">> 3.in 可以查看成员对象是否存在\n",
    "\n",
    "> 列表的一些方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "79183a31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "177e3719",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mary', 'Alex', 'Jack', '黄晓明'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网3_name_list = [\"Mary\",\"Alex\",\"Jack\",\"黄晓明\"]\n",
    "print(网3_name_list,type(网3_name_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dec3446c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 取出 网3_name_list 中的第一位同学的名字\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "243e1f67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看 网3_name_list 有多少位同学\n",
    "len(网3_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3aab9eea",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character '，' (U+FF0C) (Temp/ipykernel_11812/2523225513.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\11513\\AppData\\Local\\Temp/ipykernel_11812/2523225513.py\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    print(\"网3：\" ，i)\u001b[0m\n\u001b[1;37m                ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character '，' (U+FF0C)\n"
     ]
    }
   ],
   "source": [
    "for i in 网3_name_list:\n",
    "    print(\"网3：\" ，i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2a5787f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#in可以查看数据是否存在\n",
    "\"Jack\" in 网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3648ed6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in + 切片 取值 len(s) s.count)\n",
    "网3_name_list_转专业 = [\"Xiaoming\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "617013f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：一个List 可不可以出现 重复项？\n",
    "网1_age_list = [18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "63f06645",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网1_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06cd59f6",
   "metadata": {},
   "source": [
    "## 2.2列表的方法（Function) --对列表数据进行操作\n",
    "> 1.增\n",
    "\n",
    "> 2.删\n",
    "\n",
    "> 3.改\n",
    "\n",
    "> 4.查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5020eb7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "### append 增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2d9d3212",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mary', 'Alex', 'Jack', '黄晓明']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "928003d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "网3_name_list.append(\"王奕\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e1518f02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mary', 'Alex', 'Jack', '黄晓明', '王66', '王奕']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a6b954bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "网3_name_list.append(\"周诗雨\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "82b06ba4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mary', 'Alex', 'Jack', '黄晓明', '王66', '王奕', '周诗雨', '周诗雨']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5993b8f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mary', 'Jack', '黄晓明', '王66', '王奕', '周诗雨', '周诗雨']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和 in 一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Alex\" in 网3_name_list:\n",
    "    网3_name_list.remove(\"Alex\")\n",
    "网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef5f3899",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop 移除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "94e4cee8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周诗雨'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "5c1ad1d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mary', 'Jack', '黄晓明', '王66', '王奕', '周诗雨']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "f56396a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Mary'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list.pop(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "aa42c1af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', '黄晓明', '王66', '王奕', '周诗雨']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "57eb641d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'王66'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "56d32ebf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', '黄晓明', '王奕', '周诗雨']"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网3_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "0ad05937",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践：请移除19的元素，5分钟实践尝试\n",
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "7faada67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "9b28df66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "4d1f4b89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思路一:remove(value),先决条件，知道值出现的次数\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "       age_list.remove(19)\n",
    "    age_list\n",
    "age_list\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "3a163c19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "d689820b",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "0da1ba54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "6 19\n"
     ]
    }
   ],
   "source": [
    "#思路二： pop(index), 先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "     if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "f6874831",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.pop(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "2bb6af36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17]"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2f88796",
   "metadata": {},
   "outputs": [],
   "source": [
    "#课后练习：课本P68和P81"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5a33306d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f701257f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d35c2590",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "new_phrase = ''.join(plist[1:3])\n",
    "new_phrase = new_phrase+''.join([plist[5],plist[4],plist[7],plist[6]])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02035ed2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
